ConfirmSale = new Object();

//custom Vtype for vtype:'Rate'
Ext.apply(Ext.form.field.VTypes, {
	Rate : function(v) {
		return /^100(\.0{0,2})? *%?$|^\d{1,2}(\.\d{1,2})? *%?$/.test(v);
	},
	RateText : 'Invalid Rate',
	RateMask : /[\d\.%]/i
});

var confsearchResultStore = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'dealCode', 'fund', 'instrumentType', 'insId', 'purchDeal', 'valueDate', 'buyer', 'secMarket', 'secBuyerName', 'secBuyerBankAc', 'settlementCode',
			'delevarymethode', 'dealerCode', 'sellingValue', 'sellingRate', 'placementFee', 'partialSale', 'sellingAmount', 'user' ]
});

var confirmSalePrevPurchStore = Ext.create('Ext.data.Store', {
	fields : [ {
		name : 'id',
		type : 'int'
	}, {
		name : 'dealCode',
		type : 'string'
	}, {
		name : 'dealName',
		type : 'string'
	} ]
});

ConfirmSale.getSearchBar = function() {

	return Ext.create('Ext.form.FieldContainer', {
		items : [ {
			xtype : 'triggerfield',
			id : 'confirmSaleTrigField',
			triggerCls : 'x-form-search-trigger',
			anchor : '28%',
			style : 'float:right;',
			emptyText : 'Search for a Purchase',
			listeners : {
				specialkey : function(field, e) {
					if (e.getKey() == e.ENTER) {
						Ext.Msg.alert('HI', 'Press Enter');
					}
				}
			},
			onTriggerClick : function() {
				var searchResult = Ext.create('Ext.data.Store', {
					fields : [ 'id', 'dealCode', 'issuer', 'status' ]
				});
				var TrigField = Ext.ComponentQuery.query('#confirmSaleTrigField')[0];

				Ext.Ajax.request({
					url : 'getSales.action',
					params : {
						queryOne : TrigField.getValue(),
					},
					success : function(response) {
						var obj = Ext.decode(response.responseText);
						var arrayLength = obj.deals.length;
						for (var i = 0; i < arrayLength; i++) {
							searchResult.add({
								id : obj.deals[i].id,
								dealCode : obj.deals[i].dealCode,
								issuer : obj.deals[i].issuer.code,
								name : obj.deals[i].name,
								status : obj.deals[i].status,
							});
						}
					}
				});

				var resultWindow = Ext.create('Ext.grid.Panel', {
					defaults : {
						anchor : '100%'
					},
					store : searchResult,
					columns : [ {
						text : 'Deal Code',
						dataIndex : 'dealCode',
						flex : 1
					}, {
						text : 'Issuer',
						dataIndex : 'issuer',
						flex : 1
					}, {
						text : 'Status',
						dataIndex : 'status',
						flex : 1
					} ],

					listeners : {
						itemclick : function(view, rec, item, index, eventObj) {
							var findId = rec.get('id');
							var formPanl = Ext.ComponentQuery.query('#confirmSaleFormPanel')[0];
							formPanl.setLoading('Receiving data...', formPanl.getForm());
							var confirmFields = '';
							var rejectBtn = Ext.getCmp('confirmSaleRejectBtn');
							var confirmBtn = Ext.getCmp('confirmSaleConfirmBtn');

							formPanl.getForm().reset();

							Ext.Ajax.request({
								url : 'getSale.action',
								params : {
									id : findId,
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									confsearchResultStore.loadRawData(obj.saleDealDtos);
									var recor = confsearchResultStore.getAt(0);
									Ext.Ajax.request({
										url : 'getInstrumById.action',
										params : {
											'instrumentId' : recor.get('insId')
										},
										success : function(response) {
											var obj = Ext.decode(response.responseText);
											Ext.data.StoreManager.lookup('confirmSaleInsStore').loadRawData(obj.instrumentDtos);
										},
										failure : function(form, action) {
											formPanl.setLoading(false);
											Ext.Msg.alert("An Error occured. Load failed");
										}
									});

									Ext.Ajax.request({
										url : 'getPurchdealById.action',
										params : {
											purchDeal : recor.get('purchDeal')
										},
										success : function(response) {
											var obj = Ext.decode(response.responseText);
											if (obj.queryTwo != null && obj.queryTwo != '') {
												var purchArray = obj.queryTwo.split(";");
												confirmSalePrevPurchStore.removeAll();
												var i = 0;
												for (i = 0; i < purchArray.length - 1; i++) {
													var purchDealArr = purchArray[i].split(",");
													confirmSalePrevPurchStore.add({
														id : purchDealArr[0],
														dealCode : purchDealArr[1],
														dealName : purchDealArr[2]
													});
												}
												confirmSalePrevPurchStore.sort('id', 'ASC');
												formPanl.loadRecord(recor);

												Ext.getCmp('confirmSaleDealCode').setText('Deal Code: ' + recor.get('dealCode'));

												if (rec.get('status') == 'Initial') {
													Ext.Ajax.request({
														url : 'confFields.action',
														params : {
															screen : 'RecordSale',
														},
														success : function(response) {
															var objFive = Ext.decode(response.responseText);
															confirmFields = objFive.values;

															formPanl.getForm().getFields().each(function(field) {
																field.setDisabled(true);
															});

															var confFieldsArrray = confirmFields.split(";");
															var i;
															var confFieldLocal;
															var confFieldQuery;
															for (i = 0; i < confFieldsArrray.length - 1; i++) {
																confFieldQuery = '#' + confFieldsArrray[i];
																confFieldLocal = Ext.ComponentQuery.query(confFieldQuery)[0];
																confFieldLocal.enable();
																confFieldLocal.reset();
															}

															Ext.ComponentQuery.query('#confirmSaleTrigField')[0].enable();
														},
														failure : function(form, action) {
															Ext.Msg.alert('Error!', 'Failed to load Confirmation Fields!');
														}
													});
													rejectBtn.enable();
													confirmBtn.enable();

												} else {
													formPanl.getForm().getFields().each(function(field) {
														field.setDisabled(true);
													});
													rejectBtn.disable();
													confirmBtn.disable();
													Ext.ComponentQuery.query('#confirmSaleTrigField')[0].enable();
												}

												formPanl.setLoading(false);
											}
										},
										failure : function(response) {
											formPanl.setLoading(false);
											Ext.Msg.alert('Error!', 'Failed to load Previous Purchase Deal List : ' + response);
										},
									});
								},
								failure : function(form, action) {
									formPanl.setLoading(false);
								}
							});
							this.up('.window').close();
							formPanl.setLoading(false);
						}
					}
				});

				Ext.create('Ext.window.Window', {
					title : 'Sales',
					id : 'confirmSaleResWindow',
					height : 330,
					frame : true,
					width : 500,
					constrain : true,
					resizable : false,
					layout : {
						type : 'fit'
					},
					items : [ resultWindow ]
				}).show();
			}
		} ]
	});
};
ConfirmSale.getFieldSet1 = function() {

	var confirmSaleInsTypes = Ext.create('Ext.data.Store', {
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'instrumentName',
			type : 'string',
		}, {
			name : 'istrumentType',
			type : 'string',
		} ],
		storeId : 'confirmSaleInsTypeStore',
	});

	var confirmSaleFundStore = Ext.create('Ext.data.Store', {
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'fundName',
			type : 'string',
		}, {
			name : 'fundCode',
			type : 'string',
		} ],
	});

	Ext.Ajax.request({
		url : 'getFundsForDeal.action',
		params : {
			querry : 'SALE',
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			confirmSaleFundStore.loadRawData(obj.fundList);
		}
	});

	var confirmSaleInsStore = Ext.create('Ext.data.Store', {
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'instrumentCode',
			type : 'string',
		} ],
		storeId : 'confirmSaleInsStore',
	});

	var selectInsComboBox = Ext.create('Ext.form.ComboBox', {
		name : 'insId',
		fieldLabel : 'Instrument Code',
		id : 'confirmSaleInsCodeCmb',
		store : confirmSaleInsStore,
		queryMode : 'local',
		valueField : 'id',
		displayField : 'instrumentCode',
		autoScroll : true,
		allowBlank : false,
		flex : 1,
		padding : '0 5 0 5',
		style : 'margin-top:5px;',
		blankText : 'Please select an Instrument to proceed',
		msgTarget : 'under',
		listeners : {
			select : {
				fn : function(combo, value) {
					Ext.Ajax.request({
						url : 'prevPurchases.action',
						params : {
							queryOne : combo.getValue(),
						},
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							if (obj.queryTwo != null && obj.queryTwo != '') {
								var purchArray = obj.queryTwo.split(";");
								confirmSalePrevPurchStore.removeAll();
								var i = 0;
								for (i = 0; i < purchArray.length - 1; i++) {
									var purchDealArr = purchArray[i].split(",");
									confirmSalePrevPurchStore.add({
										id : purchDealArr[0],
										dealCode : purchDealArr[1],
										dealName : purchDealArr[2]
									});
								}
								confirmSalePrevPurchStore.sort('id', 'ASC');
							}
						},
						failure : function(response) {
							Ext.Msg.alert('Error!', 'Failed to load Previous Purchase Deal List : ' + response);
						},
					});
				}
			}
		}

	});

	var confirmSaleIssuerStore = Ext.create('Ext.data.Store', {
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'name',
			type : 'string',
		}, {
			name : 'code',
			type : 'string',
		} ],
		storeId : 'confirmSaleIssuerStore',
	});

	Ext.Ajax.request({
		url : 'getIssuers.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			confirmSaleIssuerStore.loadRawData(obj.issuerList);
		},
		failure : function(response) {
			Ext.Msg.alert('Error!', 'Failed to Load Issuers!');
		}

	});

	var selectBuyerComboBox = Ext.create('Ext.form.ComboBox', {
		name : 'buyer',
		fieldLabel : 'Buyer',
		id : 'confirmSaleIssuerCmb',
		store : confirmSaleIssuerStore,
		queryMode : 'local',
		valueField : 'id',
		autoScroll : true,
		allowBlank : false,
		flex : 1,
		padding : '0 5 0 5',
		style : 'margin-top:5px;',
		blankText : 'Please select an Issuer to proceed',
		msgTarget : 'under',
		tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{name} - <b>{code}</b><br/></div>', '</tpl>'),
		displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>'),
	});

	var selectInsTypeComboBox = Ext.create('Ext.form.ComboBox', {
		name : 'instrumentType',
		fieldLabel : 'Instrument Type',
		id : 'confirmSaleInsTypeCmb',
		store : confirmSaleInsTypes,
		queryMode : 'local',
		valueField : 'id',
		displayField : 'instrumentName',
		autoScroll : true,
		allowBlank : false,
		flex : 1,
		padding : '0 5 0 5',
		style : 'margin-top:5px;',
		blankText : 'Please select an Instrument Type to proceed',
		msgTarget : 'under',
		editable : false,
		listeners : {
			select : {
				fn : function(combo, value) {
					var govLbl = Ext.ComponentQuery.query('#confirmSaleGovLbl')[0];
					var issuerCmb = Ext.ComponentQuery.query('#confirmSaleIssuerCmb')[0];
					Ext.ComponentQuery.query('#confirmSaleInsCodeCmb')[0].clearValue();
					confirmSaleInsStore.removeAll();
					Ext.Ajax.request({
						url : 'InsFromTypes.action',
						params : {
							queryOne : combo.getValue(),
							queryTwo : Ext.ComponentQuery.query('#confirmSaleFundCmb')[0].getValue(),
						},
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							Ext.data.StoreManager.lookup('confirmSaleInsStore').loadRawData(obj.insList);
							var insTypesStoreTmp = Ext.data.StoreManager.lookup('confirmSaleInsTypeStore');
							if (insTypesStoreTmp.getAt(insTypesStoreTmp.find('id', combo.getValue())).get('istrumentType') === 'G') {
								govLbl.setText('Issuer Type: Government');
								issuerCmb.clearValue();
							} else {
								govLbl.setText('Issuer Type: Non-Government');
							}
						},
						failure : function(response) {
							Ext.Msg.alert('Error!', 'Failed to Load Instruments!');
						}
					});
				}
			}
		}
	});

	var finalFieldSet = Ext.create('Ext.form.FieldSet', {
		title : 'Basic',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'label',
			id : 'confirmSaleDealCode',
			style : 'font-weight:600;margin-top:7px;',
			name : 'dealCode'
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ Ext.create('Ext.form.ComboBox', {
				name : 'fund',
				id : 'confirmSaleFundCmb',
				fieldLabel : 'Fund',
				store : confirmSaleFundStore,
				valueField : 'id',
				autoScroll : true,
				allowBlank : false,
				queryMode : 'local',
				flex : 1,
				padding : '0 5 0 5',
				style : 'margin-top:10px;',
				blankText : 'Please select a Fund to proceed',
				msgTarget : 'under',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{fundName} - <b>{fundCode}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{fundName}', '</tpl>'),
				listeners : {
					select : {
						fn : function(combo, value) {
							Ext.ComponentQuery.query('#confirmSaleInsTypeCmb')[0].clearValue();
							Ext.ComponentQuery.query('#confirmSaleInsCodeCmb')[0].clearValue();
							confirmSaleInsStore.removeAll();
							confirmSaleInsTypes.removeAll();
							Ext.Ajax.request({
								url : 'dealInsTypes.action',
								params : {
									queryOne : combo.getValue(),
									queryTwo : 'SALE',
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									Ext.data.StoreManager.lookup('confirmSaleInsTypeStore').loadRawData(obj.insTypeList);
								},
								failure : function(response) {
									Ext.Msg.alert('Error!', 'Failed to Load Instrument Types!');
								}
							});

						}
					},
					change : function(list, record) {
						Ext.Ajax.request({
							url : 'dealInsTypes.action',
							params : {
								queryOne : list.getValue(),
								queryTwo : 'SALE',
							},
							success : function(response) {
								var obj = Ext.decode(response.responseText);
								Ext.data.StoreManager.lookup('confirmSaleInsTypeStore').loadRawData(obj.insTypeList);
							},
							failure : function(response) {
								Ext.Msg.alert('Error!', 'Failed to Load Instrument Types!');
							}
						});
					}
				}
			}), selectInsTypeComboBox ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ selectInsComboBox, Ext.create('Ext.form.ComboBox', {
				name : 'purchDeal',
				fieldLabel : 'Purchase Deal',
				id : 'confirmSalePurchDealCmb',
				store : confirmSalePrevPurchStore,
				queryMode : 'local',
				valueField : 'id',
				autoScroll : true,
				allowBlank : false,
				flex : 1,
				padding : '0 5 0 5',
				style : 'margin-top:5px;',
				blankText : 'Please select an Instrument Type to proceed',
				msgTarget : 'under',
				editable : false,
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{dealCode}</b> - {dealName}<br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{dealCode}', '</tpl>'),
				listeners : {
					select : {
						fn : function(combo, value) {
							console.log(combo.getValue());
							Ext.Ajax.request({
								url : 'prevPurch.action',
								params : {
									queryOne : combo.getValue(),
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									Ext.ComponentQuery.query('#recSalePreValue')[0].setValue(obj.preDealVal);
									Ext.ComponentQuery.query('#recSalePreIssuer')[0].setValue(obj.preIssuer);
									if (obj.preSecMarket) {
										Ext.ComponentQuery.query('#recSalePreSecMarket')[0].setValue(true);
										if (obj.preSecSeller != null) {
											Ext.ComponentQuery.query('#recSalePreSecSeller')[0].setValue(obj.preSecSeller);
										} else {
											Ext.ComponentQuery.query('#recSalePreSecSeller')[0].reset();
										}
									} else {
										Ext.ComponentQuery.query('#recSalePreSecMarket')[0].setValue(false);
										Ext.ComponentQuery.query('#recSalePreSecSeller')[0].reset();
									}
									if (obj.preCpnRate != null) {
										Ext.ComponentQuery.query('#recSaleCouponRate')[0].setValue(obj.preCpnRate);
										if (obj.preCpnDateFixed) {
											Ext.ComponentQuery.query('#recSaleCpnFixcb')[0].setValue(true);
										} else {
											Ext.ComponentQuery.query('#recSaleCpnVarycb')[0].setValue(true);
										}
									} else {
										Ext.ComponentQuery.query('#recSaleCouponRate')[0].reset();
										Ext.ComponentQuery.query('#recSaleCpnFixcb')[0].reset();
										Ext.ComponentQuery.query('#recSaleCpnVarycb')[0].reset();
									}

									if (obj.preInterestFixed) {
										Ext.ComponentQuery.query('#recSaleIntTypeFix')[0].setValue(true);
									} else {
										Ext.ComponentQuery.query('#recSaleIntTypeFloat')[0].setValue(true);
									}

									if (obj.preInterestStraight) {
										Ext.ComponentQuery.query('#recSaleIntStraight')[0].setValue(true);
									} else {
										Ext.ComponentQuery.query('#recSaleIntDiscount')[0].setValue(true);
									}
									if (obj.sellingAmount != null) {
										Ext.ComponentQuery.query('#recSaleSellingAmount')[0].setValue(obj.sellingAmount);
									}

									if (obj.preScripts) {
										Ext.ComponentQuery.query('#recSaleScript')[0].setValue(true);
									} else {
										Ext.ComponentQuery.query('#recSaleScript')[0].setValue(false);
									}

									if (obj.preDenominals) {
										Ext.ComponentQuery.query('#recSaleDen')[0].setValue(true);
										Ext.ComponentQuery.query('#recSalePartialSale')[0].setValue(false);
										Ext.ComponentQuery.query('#recSalePartialSale')[0].enable();
										if (obj.preDenMinVal != null) {
											Ext.ComponentQuery.query('#recSaleDenMinVal')[0].setValue(obj.preDenMinVal);
										}
									} else {
										Ext.ComponentQuery.query('#recSaleDen')[0].setValue(false);
										Ext.ComponentQuery.query('#recSaleDenMinVal')[0].reset();
										Ext.ComponentQuery.query('#recSalePartialSale')[0].setValue(false);
										Ext.ComponentQuery.query('#recSalePartialSale')[0].disable();
									}

									Ext.ComponentQuery.query('#recSalePreRate')[0].setValue(obj.preRate);
									Ext.ComponentQuery.query('#recSalePreDealCode')[0].setText('Purchase Deal Code: ' + obj.preDealCode);

								},
								failure : function(response) {
									Ext.Msg.alert('Error!', 'Failed to Load Purchase Details!');
								}
							});
						}
					},
					change : function(list, record) {
						if (record) {
							Ext.Ajax.request({
								url : 'prevPurch.action',
								params : {
									queryOne : list.getValue(),
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									Ext.ComponentQuery.query('#confirmSalePreValue')[0].setValue(obj.preDealVal);
									Ext.ComponentQuery.query('#confirmSalePreIssuer')[0].setValue(obj.preIssuer);
									if (obj.preSecMarket) {
										Ext.ComponentQuery.query('#confirmSalePreSecMarket')[0].setValue(true);
										if (obj.preSecSeller != null) {
											Ext.ComponentQuery.query('#confirmSalePreSecSeller')[0].setValue(obj.preSecSeller);
										} else {
											Ext.ComponentQuery.query('#confirmSalePreSecSeller')[0].reset();
										}
									} else {
										Ext.ComponentQuery.query('#confirmSalePreSecMarket')[0].setValue(false);
										Ext.ComponentQuery.query('#confirmSalePreSecSeller')[0].reset();
									}

									if (obj.preCpnRate != null) {
										Ext.ComponentQuery.query('#confirmSaleCouponRate')[0].setValue(obj.preCpnRate);
										if (obj.preCpnDateFixed) {
											Ext.ComponentQuery.query('#confirmSaleCpnFixcb')[0].setValue(true);
										} else {
											Ext.ComponentQuery.query('#confirmSaleCpnVarycb')[0].setValue(true);
										}
									} else {
										Ext.ComponentQuery.query('#confirmSaleCouponRate')[0].reset();
										Ext.ComponentQuery.query('#confirmSaleCpnFixcb')[0].reset();
										Ext.ComponentQuery.query('#confirmSaleCpnVarycb')[0].reset();
									}

									if (obj.preInterestFixed) {
										Ext.ComponentQuery.query('#confirmSaleIntTypeFix')[0].setValue(true);
									} else {
										Ext.ComponentQuery.query('#confirmSaleIntTypeFloat')[0].setValue(true);
									}

									if (obj.preInterestStraight) {
										Ext.ComponentQuery.query('#confirmSaleIntStraight')[0].setValue(true);
									} else {
										Ext.ComponentQuery.query('#confirmSaleIntDiscount')[0].setValue(true);
									}
									if (obj.sellingAmount != null) {
										Ext.ComponentQuery.query('#confirmSaleSellingAmount')[0].setValue(obj.sellingAmount);
									}
									if (obj.preScripts) {
										Ext.ComponentQuery.query('#confirmSaleScript')[0].setValue(true);
										if (obj.preDenominals) {
											Ext.ComponentQuery.query('#confirmSaleDen')[0].setValue(true);
											Ext.ComponentQuery.query('#confirmSalePartialSale')[0].setValue(false);
											Ext.ComponentQuery.query('#confirmSalePartialSale')[0].enable();
											if (obj.preDenMinVal != null) {
												Ext.ComponentQuery.query('#confirmSaleDenMinVal')[0].setValue(obj.preDenMinVal);
											}
										} else {
											Ext.ComponentQuery.query('#confirmSaleDen')[0].setValue(false);
											Ext.ComponentQuery.query('#confirmSaleDenMinVal')[0].reset();
											Ext.ComponentQuery.query('#confirmSalePartialSale')[0].setValue(false);
											Ext.ComponentQuery.query('#confirmSalePartialSale')[0].disable();
										}
									} else {
										Ext.ComponentQuery.query('#confirmSaleScript')[0].setValue(false);
										Ext.ComponentQuery.query('#confirmSaleDen')[0].setValue(false);
										Ext.ComponentQuery.query('#confirmSaleDenMinVal')[0].reset();
										Ext.ComponentQuery.query('#confirmSalePartialSale')[0].enable();
										Ext.ComponentQuery.query('#confirmSalePartialSale')[0].setValue(false);
									}

									Ext.ComponentQuery.query('#confirmSalePreRate')[0].setValue(obj.preRate);
									Ext.ComponentQuery.query('#confirmSalePreDealCode')[0].setText('Purchase Deal Code: ' + obj.preDealCode);

								},
								failure : function(response) {
									Ext.Msg.alert('Error!', 'Failed to Load Purchase Details!');
								}
							});
						}
					}
				}
			}) ]
		}, {
			xtype : 'label',
			id : 'confirmSaleDealStatus',
			style : 'font-weight:600;margin-top:7px;'
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'datefield',
				fieldLabel : 'Value Date',
				id : 'confirmSaleValDate',
				allowBlank : false,
				flex : 1,
				name : 'valueDate',
				padding : '0 5 0 5',
				style : 'margin-top:10px;',
				blankText : 'Please select a Start Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				emptyText : 'dd/mm/yyyy'
			}, selectBuyerComboBox ]
		}, {
			xtype : 'label',
			id : 'confirmSaleGovLbl',
			text : 'Issuer Type:',
			style : 'font-weight:600;margin-top:7px;',
			flex : 1
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Secondary Market',
				name : 'secMarket',
				id : 'confirmSaleSecMarket',
				padding : '0 5 0 5',
				style : 'margin-top:5px;',
				inputValue : '1',
				listeners : {
					change : {
						fn : function(object, Value) {
							var buyername = Ext.ComponentQuery.query('#confirmSaleBuyerName')[0];
							var buyerBankAc = Ext.ComponentQuery.query('#confirmSaleBuyerBankAc')[0];
							if (Value) {
								buyername.enable();
								buyerBankAc.enable();
							} else {
								buyername.reset();
								buyername.disable();
								buyerBankAc.reset();
								buyerBankAc.disable();
							}
						}
					}
				}
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				name : 'secBuyerName',
				allowBlank : false,
				id : 'confirmSaleBuyerName',
				flex : 1,
				padding : '0 5 0 5',
				style : 'margin-top:5px;',
				fieldLabel : "Buyer's Name",
				disabled : true
			}, {
				xtype : 'textfield',
				name : 'secBuyerBankAc',
				allowBlank : false,
				flex : 1,
				style : 'margin-top:5px;',
				padding : '0 5 0 5',
				id : 'confirmSaleBuyerBankAc',
				fieldLabel : "Bank Account",
				disabled : true
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'combo',
				fieldLabel : 'Delevary Method',
				store : CommenStore.delevaryMethodStore,
				valueField : 'id',
				flex : 1,
				id : 'confirmSaleDelevaryMethod',
				labelWidth : 120,
				name : 'delevarymethode',
				padding : '0 5 0 0',
				displayField : 'code',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{description}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {description}', '</tpl>'),
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
							combo.setValue(null);
						}
					}
				}
			}, {
				xtype : 'combo',
				fieldLabel : 'Dealer',
				store : CommenStore.dealersStore,
				valueField : 'id',
				flex : 1,
				id : 'confirmSaleDealer',
				padding : '0 5 0 0',
				name : 'dealerCode',
				displayField : 'code',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{name}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {name}', '</tpl>'),
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
							combo.setValue(null);
						}
					}
				}
			} ]
		}, {
			xtype : 'combo',
			labelWidth : 120,
			fieldLabel : 'Settlement Method',
			store : CommenStore.settlmentStore,
			valueField : 'id',
			id : 'confirmSaleSettlementMethod',
			anchor : '50%',
			name : 'settlementCode',
			padding : '0 10 0 0',
			displayField : 'code',
			tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{description}</b><br/></div>', '</tpl>'),
			displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {description}', '</tpl>'),
			listeners : {
				change : function(combo, record, index) {
					if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
						combo.setValue(null);
					}
				}
			}
		} ]
	});
	return finalFieldSet;
};

ConfirmSale.getFieldSet2 = function() {

	var finalFieldSet = Ext.create('Ext.form.FieldSet', {
		title : 'Instrument Infomation',
		defaults : {
			labelWidth : 175,
			anchor : '100%'
		},
		items : [ {
			xtype : 'label',
			id : 'confirmSalePreDealCode',
			style : 'font-weight:600;margin-top:7px;',
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				fieldLabel : 'Purchase Value',
				id : 'confirmSalePreValue',
				padding : '0 5 5 5',
				disabled : true,
				flex : 1
			}, {
				xtype : 'textfield',
				id : 'confirmSalePreIssuer',
				fieldLabel : "Issuer",
				padding : '0 5 5 5',
				disabled : true,
				flex : 1
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Secondary Market',
				padding : '0 5 5 5',
				flex : 1,
				disabled : true,
				id : 'confirmSalePreSecMarket',
			}, {
				xtype : 'textfield',
				name : 'secSellerName',
				id : 'confirmSalePreSecSeller',
				fieldLabel : "Seller's Name",
				flex : 1,
				padding : '0 5 5 0',
				disabled : true
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				id : 'confirmSaleCouponRate',
				fieldLabel : 'Coupon Rate',
				padding : '0 5px 5px 5px',
				disabled : true
			}, {
				xtype : 'fieldcontainer',
				fieldLabel : 'Coupon Dates',
				defaultType : 'checkboxfield',
				layout : 'hbox',
				padding : '0 5px 5px 10px',
				disabled : true,
				items : [ {
					boxLabel : 'Fixed',
					id : 'confirmSaleCpnFixcb',
					padding : '0 5px 0 5px',
				}, {
					boxLabel : 'Vary',
					id : 'confirmSaleCpnVarycb',
					padding : '0 5px 0 5px',
				} ]
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'fieldcontainer',
				fieldLabel : 'Interest Rate',
				layout : 'hbox',
				padding : '0 5px 5px 5px',
				flex : 1,
				disabled : true,
				items : [ {
					xtype : 'radiofield',
					boxLabel : 'Fixed',
					id : 'confirmSaleIntTypeFix',
					disabled : true,
					padding : '0 5px 0 5px',
				}, {
					xtype : 'radiofield',
					boxLabel : 'Floating',
					id : 'confirmSaleIntTypeFloat',
					disabled : true,
					padding : '0 5px 0 5px',
				} ]
			}, {
				xtype : 'fieldcontainer',
				fieldLabel : 'Calculation',
				layout : 'hbox',
				flex : 1,
				padding : '0 5px 5px 5px',
				disabled : true,
				items : [ {
					xtype : 'radiofield',
					id : 'confirmSaleIntStraight',
					boxLabel : 'Straight',
					inputValue : 'straight',
					padding : '0 5px 0 5px',
				}, {
					xtype : 'radiofield',
					id : 'confirmSaleIntDiscount',
					boxLabel : 'Discounted',
					inputValue : 'discounted',
					padding : '0 5px 0 5px',
				} ]
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Scripts',
				padding : '5px 5px 5px 5px',
				flex : 1,
				id : 'confirmSaleScript',
				disabled : true,
			}, {
				xtype : 'textfield',
				fieldLabel : 'Rate',
				padding : '5px 5px 5px 0',
				flex : 1,
				id : 'confirmSalePreRate',
				disabled : true,
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Denominals',
				padding : '0 5px 5px 5px',
				flex : 1,
				id : 'confirmSaleDen',
				disabled : true,
			}, {
				xtype : 'textfield',
				fieldLabel : 'Minimum Value',
				padding : '0 5px 5px 0',
				flex : 1,
				id : 'confirmSaleDenMinVal',
				disabled : true,
			} ]
		} ]
	});
	return finalFieldSet;
};

ConfirmSale.getFieldSet3 = function() {

	var finalFieldSet = Ext.create('Ext.form.FieldSet', {
		title : 'Sale Information',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'vbox',
			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'textfield',
					id : 'confirmSaleDealVal',
					minValue : 0,
					name : 'sellingValue',
					fieldLabel : 'Selling Value',
					allowBlank : false,
					blankText : 'Please enter the Deal Amount',
					msgTarget : 'under',
					flex : 1,
					padding : '0 5 5 0',
					maskRe : /[\d\,.]/,
					listeners : {
						blur : function() {
							this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
						},
						render : function(p) {
							this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
						}
					}
				}, {
					xtype : 'textfield',
					vtype : 'Rate',
					validateOnBlur : true,
					maskRe : /[\d\%.]/,
					id : 'confirmSaleSallingRate',
					flex : 1,
					padding : '0 5 5 0',
					name : 'sellingRate',
					fieldLabel : 'Selling Rate',
					listeners : {
						blur : function() {
							var value = Ext.util.Format.number(this.value.replace(/%/g, ''), '000.00');
							this.setRawValue(value + "%");
						}
					}
				} ]
			}, {
				xtype : 'textfield',
				minValue : 0,
				id : 'confirmSalePlacementFee',
				name : 'placementFee',
				fieldLabel : 'Placement Fee',
				allowBlank : false,
				blankText : 'Please enter the Placement Fee',
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			}, {
				xtype : 'checkboxfield',
				boxLabel : 'Partial Sale',
				padding : '0 5px 5px 0',
				flex : 1,
				id : 'confirmSalePartialSale',
				disabled : true,
				listeners : {
					change : function(field, newVal) {
						if (newVal) {
							var denomi = Ext.ComponentQuery.query('#confirmSaleSellingAmount')[0].enable();
						} else {
							var denomi = Ext.ComponentQuery.query('#confirmSaleSellingAmount')[0].disable();
						}
					}
				}
			}, {
				xtype : 'textfield',
				fieldLabel : 'Selling Amount',
				padding : '0 5px 5px 0',
				flex : 1,
				id : 'confirmSaleSellingAmount',
				disabled : true,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value.replace(/,/g, ''), '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			} ]
		} ]
	});
	return finalFieldSet;
};

ConfirmSale.getCenterPanel = function() {
	var filedset1 = ConfirmSale.getFieldSet1();
	var filedset2 = ConfirmSale.getFieldSet2();
	var filedset3 = ConfirmSale.getFieldSet3();
	var searchBar = ConfirmSale.getSearchBar();

	CommenStore.settlmentStore.load();
	CommenStore.dealersStore.load();
	CommenStore.delevaryMethodStore.load();
	var formPanel = Ext.create('Ext.form.FormPanel', {
		bodyPadding : '20',
		id : 'confirmSaleFormPanel',
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 175,
			anchor : '68%',
		},
		fieldDefaults : {
			labelAlign : 'left',
			labelWidth : 150,
		},
		title : 'Confirm SALE',
		items : [ searchBar, filedset1, filedset2, filedset3 ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				Ext.ComponentQuery.query('#confirmSaleDealCode')[0].setText('');
				Ext.ComponentQuery.query('#confirmSaleDealStatus')[0].setText('');
				Ext.ComponentQuery.query('#confirmSalePreDealCode')[0].setText('');
				Ext.ComponentQuery.query('#confirmSaleGovLbl')[0].setText('');
				Ext.getCmp('confirmSaleConfirmBtn').disable();
				Ext.getCmp('confirmSaleRejectBtn').disable();
				App.Core.setScreen(ConfirmSale.getCenterPanel());
			}
		}, {
			text : 'Reject',
			id : 'confirmSaleRejectBtn',
			itemId : 'rejectButton',
			disabled : true,
			handler : function() {
				var form = this.up('form').getForm();
				form.url = 'rejectSale.action';
				formPanel.setLoading('Rejecting...');
				formPanel.getForm().getFields().each(function(field) {
					field.setDisabled(true);
				});
				var dealCode = Ext.ComponentQuery.query('#confirmSaleDealCode')[0].text.substr(11);
				console.log('dealCode: ' + dealCode);

				form.submit({
					params : {
						'dealCode' : dealCode,
						'user' : 'updateUser',
					},
					success : function(form, action) {
						form.reset();
						Ext.Msg.alert('Success', dealCode + ' is Rejected.');
						Ext.ComponentQuery.query('#confirmSaleDealCode')[0].setText('');
						Ext.ComponentQuery.query('#confirmSaleDealStatus')[0].setText('');
						Ext.ComponentQuery.query('#confirmSalePreDealCode')[0].setText('');
						Ext.ComponentQuery.query('#confirmSaleGovLbl')[0].setText('');
						formPanel.getForm().getFields().each(function(field) {
							field.setDisabled(true);
						});
						Ext.getCmp('confirmSaleConfirmBtn').disable();
						Ext.getCmp('confirmSaleRejectBtn').disable();
						Ext.ComponentQuery.query('#confirmSaleTrigField')[0].enable();
						formPanel.setLoading(false);
					},
					failure : function(form, action) {
						Ext.Msg.alert('Failed', 'There is some error returned from the server.');
						Ext.ComponentQuery.query('#confirmSaleDealCode')[0].setText('');
						Ext.ComponentQuery.query('#confirmSaleDealStatus')[0].setText('');
						Ext.ComponentQuery.query('#confirmSalePreDealCode')[0].setText('');
						Ext.ComponentQuery.query('#confirmSaleGovLbl')[0].setText('');
						formPanel.getForm().getFields().each(function(field) {
							field.setDisabled(true);
						});
						Ext.getCmp('confirmSaleConfirmBtn').disable();
						Ext.getCmp('confirmSaleRejectBtn').disable();
						Ext.ComponentQuery.query('#confirmSaleTrigField')[0].enable();
						formPanel.setLoading(false);
					}
				});

			}
		}, {
			text : 'Confirm',
			id : 'confirmSaleConfirmBtn',
			itemId : 'confirmButton',
			disabled : true,
			handler : function() {
				var form = this.up('form').getForm();
				form.url = 'confSale.action';

				Ext.ComponentQuery.query('#confirmSaleFundCmb')[0].enable();
				Ext.ComponentQuery.query('#confirmSaleValDate')[0].enable();
				Ext.ComponentQuery.query('#confirmSaleInsTypeCmb')[0].enable();
				Ext.ComponentQuery.query('#confirmSaleInsCodeCmb')[0].enable();
				Ext.ComponentQuery.query('#confirmSaleIssuerCmb')[0].enable();
				Ext.ComponentQuery.query('#confirmSaleSecMarket')[0].enable();
				if (Ext.ComponentQuery.query('#confirmSaleSecMarket')[0].getValue === 1) {
					Ext.ComponentQuery.query('#confirmSaleBuyerName')[0].enable();
					Ext.ComponentQuery.query('#confirmSaleBuyerBankAc')[0].enable();
				}

				Ext.ComponentQuery.query('#confirmSaleDealVal')[0].enable();
				Ext.ComponentQuery.query('#confirmSaleSallingRate')[0].enable();
				Ext.ComponentQuery.query('#confirmSalePlacementFee')[0].enable();

				if (form.isValid()) {
					formPanel.setLoading('Confirming...');
					var valueDtField = Ext.ComponentQuery.query('#confirmSaleValDate')[0];
					var dealCode = Ext.ComponentQuery.query('#confirmSaleDealCode')[0].text.substr(11);
					form.submit({
						params : {
							'valueDate' : Ext.util.Format.date(valueDtField.getValue()),
							'user' : 'confirmedUser',
							'dealCode' : dealCode,
						},
						success : function(form, action) {
							form.reset();
							Ext.ComponentQuery.query('#confirmSaleDealCode')[0].setText('');
							Ext.ComponentQuery.query('#confirmSaleDealStatus')[0].setText('');
							Ext.ComponentQuery.query('#confirmSalePreDealCode')[0].setText('');
							Ext.ComponentQuery.query('#confirmSaleGovLbl')[0].setText('');
							Ext.getCmp('confirmSaleConfirmBtn').disable();
							Ext.getCmp('confirmSaleRejectBtn').disable();
							Ext.Msg.alert('Success', action.result.name + ' is confirmed.<br> Deal Code <b>' + action.result.dealCode + '</b>');
							formPanel.setLoading(false);
						},
						failure : function(form, action) {
							form.reset();
							Ext.ComponentQuery.query('#confirmSaleDealCode')[0].setText('');
							Ext.ComponentQuery.query('#confirmSaleDealStatus')[0].setText('');
							Ext.ComponentQuery.query('#confirmSalePreDealCode')[0].setText('');
							Ext.ComponentQuery.query('#confirmSaleGovLbl')[0].setText('');
							Ext.getCmp('confirmSaleConfirmBtn').disable();
							Ext.getCmp('confirmSaleRejectBtn').disable();
							Ext.Msg.alert('Failed', 'There is some error returned from the server.<br><b>' + action.result.status + '</b>');
							formPanel.setLoading(false);
						}
					});
				} else {
					Ext.Msg.alert("ERROR!", "Form Incomplete. Please check!");
				}
			}
		} ]
	});
	return formPanel;
};
